home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Storage / DragPriv.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  12.9 KB  |  437 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DragPriv.cpp
  3.  
  4.     Contains:    Implementation of Private classes for ODDragAndDrop.
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     3/15/96    DH        1287259 - 1.0.2 Don't get back what I put
  13.                                     in the scrapbook. Changed ODMemContainer so
  14.                                     that it has a constructor that makes a new
  15.                                     container given a container handle.
  16.         <27>    10/13/95    EL        1287340: Use standard ISO prefix
  17.         <26>     10/4/95    jpa        Added missing ODVolatiles [1285799]
  18.         <25>     10/4/95    VL        1287585: Check to see whether we are
  19.                                     dragging a folder before getting the file
  20.                                     info.
  21.         <24>     9/13/95    VL        1283523: Stop copying names for stationery
  22.                                     files.
  23.         <23>      9/8/95    VL        1274572: Preserved the curResFile in
  24.                                     GetIconFamilyFromFile.
  25.         <22>      9/8/95    TÇ        1281096 FB2:Many constants in ODTypesB
  26.                                     without kOD prefix!
  27.         <21>      9/6/95    VL        1274572: Added GetFinderInfo and
  28.                                     GetIconFamilyFromFile.
  29.         <20>     8/12/95    TÇ        1276812 Need to use TempObjs and TempRefs
  30.                                     for exception safety and to avoid TRY
  31.                                     blocks, 1276807 Opt./Bug: use StdTypIO
  32.                                     routines for portable streaming & smaller
  33.                                     footprint
  34.         <19>     7/26/95    VL        1270320: ClearAllPromises when deleting
  35.                                     memory container.
  36.         <18>     5/26/95    VL        1251403: Multithreading naming support.
  37.         <17>     5/25/95    jpa        Fixed usage of ODDebug. [1253321]
  38.         <16>     4/25/95    VL        1240737: Make sure that ID for file
  39.                                     container is set to the right _length.
  40.         <15>     4/15/95    VL        1240014: turned off debug statements.
  41.         <14>     4/14/95    VL        Temporarily turned on debug printf.
  42.         <13>     3/10/95    VL        1227218: Get rid of system heap.
  43.         <12>     2/27/95    VL        1198037: Removed debugstr from code.
  44.         <11>     2/10/95    VL        1205627: Used kODBentoUpdateContainer.
  45.         <10>     1/26/95    VL        #???: Use updated Storage Unit Ref API.
  46.          <9>     1/19/95    CC        1212419 Pass kODBentoMemoryContainer to
  47.                                     CreateMemoryContainer & GetMemoryContainer.
  48.          <8>    12/20/94    VL        1195012: Make Storage calls be
  49.                                     marshallable.
  50.          <7>    12/13/94    VL        1203627,1200603,1203451,1198037,1194537,119
  51.                                     4755,1186815: Bug fixes.
  52.          <6>     9/23/94    VL        1184272: ContainerID is now a sequence of
  53.                                     octets.
  54.          <5>     8/26/94    VL        1183174: Use updated cloning APIs
  55.                                     (specifically GetStorageUnitRef takes ID
  56.                                     now).
  57.          <4>     8/15/94    TÇ        #1180922 Removed most obsolete types from
  58.                                     StdTypes.idl
  59.          <3>      8/3/94    VL        1153123: Storage to ODStor.
  60.          <2>     7/26/94    VL        Got rid of dependency on Bento.
  61.          <1>     7/21/94    VL        first checked in
  62.  
  63.     To Do:
  64.     In Progress:
  65. */
  66.  
  67. #ifndef _DRAGPRIV_
  68. #include <DragPriv.h>
  69. #endif
  70.  
  71. #ifndef SOM_ODStorageSystem_xh
  72. #include <ODStor.xh>
  73. #endif
  74.  
  75. #ifndef SOM_ODContainer_xh
  76. #include <ODCtr.xh>
  77. #endif
  78.  
  79. #ifndef SOM_ODDocument_xh
  80. #include <Document.xh>
  81. #endif
  82.  
  83. #ifndef SOM_ODDraft_xh
  84. #include <ODDraft.xh>
  85. #endif
  86.  
  87. #ifndef SOM_ODStorageUnit_xh
  88. #include <StorageU.xh>
  89. #endif
  90.  
  91. #ifndef _ODMEMORY_
  92. #include "ODMemory.h"
  93. #endif
  94.  
  95. #ifndef _STDTYPIO_
  96. #include "StdTypIO.h"
  97. #endif
  98.  
  99. #ifndef _TEMPOBJ_
  100. #include "TempObj.h"
  101. #endif
  102.  
  103. #ifndef SOM_Module_OpenDoc_StdProps_defined
  104. #include <StdProps.xh>
  105. #endif
  106.  
  107. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  108. #include <StdTypes.xh>
  109. #endif
  110.  
  111. #ifndef _STORUTIL_
  112. #include <StorUtil.h>
  113. #endif
  114.  
  115. #ifndef _BARRAY_
  116. #include <BArray.h>
  117. #endif
  118.  
  119. #ifndef _ISOSTR_
  120. #include <ISOStr.h>
  121. #endif
  122.  
  123. #ifndef _INFOUTIL_
  124. #include <InfoUtil.h>
  125. #endif
  126.  
  127. #ifndef _ICONS_
  128. #include <Icons.h>
  129. #endif
  130.  
  131. #ifndef _USERSRCM_
  132. #include <UseRsrcM.h>
  133. #endif
  134.  
  135. #ifndef __FINDER__
  136. #include <Finder.h>
  137. #endif
  138.  
  139. #if ODDebug
  140. #ifndef __STRINGS__
  141. #include <Strings.h>
  142. #endif
  143. #endif
  144.  
  145. #if ODDebug
  146. // #define ODDebug_DragPriv    1
  147. #endif
  148.  
  149. const ODContainerType kODBentoUpdateContainer = "+//ISO 9070/ANSI::113722::US::CI LABS::OpenDoc:Bento Container Suite:ContainerType:Update";
  150.  
  151. //==============================================================================
  152. // Function Prototype
  153. //==============================================================================
  154.  
  155. ODContainerType    GetContainerSuiteType(FSSpec* fsSpec);
  156.  
  157. //==============================================================================
  158. // ODDragItem
  159. //==============================================================================
  160. //------------------------------------------------------------------------------
  161. // ODDragItem::ODDragItem
  162. //------------------------------------------------------------------------------
  163. ODDragItem::ODDragItem(ODStorageSystem *storage, ODBoolean IsForeignDataSU)
  164. {
  165.     fStorageSystem = storage;
  166.     fContainer = kODNULL;
  167.     fDocument = kODNULL;
  168.     fDraft = kODNULL;
  169.     fSU = kODNULL;
  170. }
  171.  
  172. void ODDragItem::Open(Environment* ev)
  173. {
  174.     WARN("ODDragItem::Open should not be called.");
  175. }
  176.  
  177.  
  178. void ODDragItem::Close(Environment* ev)
  179. {
  180.     WARN("ODDragItem::Open should not be called.");
  181. }
  182.  
  183. //==============================================================================
  184. // ODMemDragItem
  185. //==============================================================================
  186. //------------------------------------------------------------------------------
  187. // ODMemDragItem::~ODMemDragItem
  188. //------------------------------------------------------------------------------
  189.  
  190. ODMemDragItem::ODMemDragItem(ODStorageSystem *storage, ODHandle containerH, ODBoolean IsForeignDataSU)
  191.         : ODDragItem(storage,IsForeignDataSU)
  192.     fContainerHandle = containerH; 
  193. }
  194.  
  195.  
  196. ODMemDragItem::~ODMemDragItem()
  197. {
  198.     Environment* ev = somGetGlobalEnvironment();
  199.  
  200.     if (fSU != kODNULL)
  201.     {
  202.         fSU->ClearAllPromises(ev);
  203.         fSU->Release(ev);
  204.     }
  205.     if (fDraft != kODNULL)
  206.         fDraft->Release(ev);
  207.     if (fDocument != kODNULL)
  208.         fDocument->Release(ev);
  209.     if (fContainer != kODNULL)
  210.         fContainer->Release(ev);
  211.     ODDisposeHandle(fContainerHandle);
  212. }
  213.  
  214. //------------------------------------------------------------------------------
  215. // ODMemDragItem::Initialize
  216. //------------------------------------------------------------------------------
  217. void ODMemDragItem::Initialize(Environment* ev)
  218. {
  219.     ODDragItem::Initialize(ev);
  220.     
  221.     fContainerHandle = ODNewHandle(0);
  222.     ASSERTM(MemError() == noErr, MemError(), "Error allocating D&D container handle");
  223.  
  224.     fContainer = CreateMemoryContainer(ev, fStorageSystem->GetSession(ev), fContainerHandle, kODBentoMemoryContainer);
  225.     fDocument = fContainer->AcquireDocument(ev, kODDefaultDocument);        
  226.     fDraft = fDocument->AcquireBaseDraft(ev, kODDPExclusiveWrite);
  227.     fSU = fDraft->CreateStorageUnit(ev);
  228.     TempODStorageUnit draftProperties = fDraft->AcquireDraftProperties(ev);
  229.     ODSetStrongSURefProp(ev, draftProperties, kODPropRootPartSU, kODStrongStorageUnitRef, fSU->GetID(ev));
  230. }
  231.  
  232. //------------------------------------------------------------------------------
  233. // ODMemDragItem::Open
  234. //------------------------------------------------------------------------------
  235. void ODMemDragItem::Open(Environment* ev)
  236. {
  237.     fContainer = GetMemoryContainer(ev, fStorageSystem->GetSession(ev), fContainerHandle, kODBentoMemoryContainer);
  238.     fDocument = fContainer->AcquireDocument(ev, kODDefaultDocument);
  239.     fDraft = fDocument->AcquireBaseDraft(ev, kODDPExclusiveWrite);
  240.     TempODStorageUnit    draftProperties = fDraft->AcquireDraftProperties(ev);
  241.     fSU = fDraft->AcquireStorageUnit(ev, ODGetStrongSURefProp(ev, draftProperties, kODPropRootPartSU, kODStrongStorageUnitRef));
  242. }
  243.  
  244. //------------------------------------------------------------------------------
  245. // ODMemDragItem::Close
  246. //------------------------------------------------------------------------------
  247. void ODMemDragItem::Close(Environment* ev)
  248. {
  249.     if (fSU != kODNULL) {
  250.         fSU->Release(ev);
  251.     }
  252.     if (fDraft != kODNULL) {
  253.         fDraft->Externalize(ev);
  254.         fDraft->Release(ev);
  255.     }
  256.     if (fDocument != kODNULL)
  257.         fDocument->Release(ev);
  258.     if (fContainer != kODNULL)
  259.         fContainer->Release(ev);
  260. }
  261.  
  262. //==============================================================================
  263. // ODFileDragItem
  264. //==============================================================================
  265. //------------------------------------------------------------------------------
  266. // ODFileDragItem::~ODFileDragItem
  267. //------------------------------------------------------------------------------
  268. ODFileDragItem::~ODFileDragItem()
  269. {
  270. #if ODDebug_DragPriv
  271.     somPrintf("~ODFileDragItem: %x %x %x %x\n", fSU, fDraft, fDocument, fContainer);
  272. #endif
  273.  
  274.     Environment* ev = somGetGlobalEnvironment();
  275.     
  276.     if (fSU != kODNULL)
  277.         fSU->Release(ev);
  278.     if (fDraft != kODNULL)
  279.         fDraft->Release(ev);
  280.     if (fDocument != kODNULL)
  281.         fDocument->Release(ev);
  282.     if (fContainer != kODNULL)
  283.         fContainer->Release(ev);
  284. }
  285.  
  286. //------------------------------------------------------------------------------
  287. // ODFileDragItem::Initialize
  288. //------------------------------------------------------------------------------
  289. void ODFileDragItem::Initialize(Environment* ev, ODContainerID* containerID)
  290. {
  291.     ODDragItem::Initialize(ev);
  292.     
  293.     ODStorageUnitID    suid;
  294.     ODContainerType containerType = kODNULL;    ODVolatile(containerType);
  295.         
  296.     TRY
  297.         containerType = GetContainerSuiteType((FSSpec*) containerID->_buffer);
  298.         containerID->_length = sizeof(short) + sizeof(long) + ((FSSpec*) containerID->_buffer)->name[0] + 1;
  299.         fContainer = fStorageSystem->CreateContainer(ev, containerType, containerID);
  300.         fDocument = fContainer->AcquireDocument(ev, kODDefaultDocument);        
  301.         fDraft = fDocument->AcquireBaseDraft(ev, kODDPExclusiveWrite);
  302.         TempODStorageUnit    draftProperties = fDraft->AcquireDraftProperties(ev);
  303.         suid = ODGetStrongSURefProp(ev, draftProperties, kODPropRootPartSU, kODStrongStorageUnitRef);
  304.         fSU = fDraft->AcquireStorageUnit(ev, suid);
  305.         ODDisposePtr(containerType);
  306. #if ODDebug_DragPriv
  307.     somPrintf("ODFileDragItem::Initialize %x %x %x %x\n", fSU, fDraft, fDocument, fContainer);
  308. #endif
  309.  
  310.     CATCH_ALL
  311. #ifdef ODDebug_DragPriv
  312.         ODError err = ErrorCode();
  313.         WARN("Error in ODFileDragItem::Initialize");
  314.         char    errString[100];
  315.         numtostring(err, errString);
  316.         WARN(errString);
  317. #endif
  318.         ODReleaseObject(ev,fSU);
  319.         ODReleaseObject(ev,fDraft);
  320.         ODReleaseObject(ev,fDocument);
  321.         ODReleaseObject(ev,fContainer);
  322.         ODDisposePtr(containerType);
  323.         RERAISE;
  324.     ENDTRY
  325. }
  326.  
  327. //==============================================================================
  328. // ODDragLink
  329. //==============================================================================
  330. //------------------------------------------------------------------------------
  331. // ODDragLink::ODDragLink
  332. //------------------------------------------------------------------------------
  333. ODDragLink::ODDragLink(ODDragItem *theItem, ODBoolean cleanup)
  334. {
  335.     fItem = theItem;
  336.     fCleanup = cleanup;
  337. }
  338.  
  339. //------------------------------------------------------------------------------
  340. // ODDragLink::~ODDragLink
  341. //------------------------------------------------------------------------------
  342. ODDragLink::~ODDragLink()
  343. {
  344.     if (fCleanup)
  345.         delete fItem;
  346. }
  347.  
  348. //------------------------------------------------------------------------------
  349. // GetContainerSuiteType
  350. //------------------------------------------------------------------------------
  351.  
  352. ODContainerType    GetContainerSuiteType(FSSpec* fsSpec)
  353. {
  354. #if USE_GETCONTAINER_SUITE_TYPE
  355.     PlatformFile    file;
  356.     file.Specify(fsSpec);
  357.     
  358.     ODContainerType containerType = file.AcquireContainerType();
  359.     if (ODISOStrEqual(containerType, kODBentoFileContainer) || 
  360.         ODISOStrEqual(containerType, "")) {
  361.         containerType = kODBentoUpdateContainer;
  362.     }
  363. #else
  364.     ODULong len = strlen(kODBentoUpdateContainer);
  365.     ODContainerType containerType = (ODContainerType) ODNewPtr(len + 1);
  366.     strcpy(containerType, kODBentoUpdateContainer);
  367.     return containerType;
  368. #endif
  369. }
  370.  
  371. //------------------------------------------------------------------------------
  372. // GetFinderInfo
  373. //------------------------------------------------------------------------------
  374. void GetFinderInfo(Environment* ev, ODStorageUnit* su, PlatformFile* file)
  375. {
  376.     if (!file->IsDirectory())
  377.     {
  378.         ODTime creationDate = file->GetFileCreationDate();
  379.         ODSetCreationDate(ev, su, creationDate);
  380.         ODTime modDate = file->GetFileModDate();
  381.         ODSetModificationDate(ev, su, modDate);
  382.     
  383.         if (!file->IsStationery())
  384.         {    
  385.             ODName* name = file->GetName();
  386.             ODSetPONameUsingSU(ev, su, name);
  387.             DisposeIText(name);
  388.         }
  389.     
  390.         // Stationery bit is purposedly left out.
  391.     }
  392. }
  393.  
  394. //------------------------------------------------------------------------------
  395. // GetIconFamilyFromFile
  396. //------------------------------------------------------------------------------
  397. void GetIconFamilyFromFile(Environment* ev, ODStorageUnit* su, PlatformFile* file)
  398. {
  399.     if (!file->IsDirectory())
  400.     {
  401.         Handle iconFamily;
  402.         
  403.         short refNum = 0;
  404.         ODBoolean hasCustomIcon;
  405.         OSErr err = noErr;
  406.         ODVolatile(err);
  407.         TRY
  408.             hasCustomIcon = file->HasCustomIcon();
  409.             if (hasCustomIcon)
  410.             {
  411.                 file->SetPermission(fsRdPerm);
  412.                 refNum = CurResFile();
  413.                 file->OpenResFile();
  414.             }
  415.         CATCH_ALL
  416.             UseResFile(refNum);
  417.         ENDTRY
  418.         
  419.         if (hasCustomIcon && (err == noErr))
  420.         {        
  421.             err = GetIconSuite(&iconFamily, kCustomIconResource, svAllAvailableData);
  422.             if (err == noErr)
  423.             {
  424.                 ODSetIconFamilyProp(ev, su, kODPropCustomIcon, kODIconFamily, iconFamily, kODFalse);
  425.                 DisposeIconSuite(iconFamily, true);
  426.             }
  427. #ifdef ODDEBUG
  428.             else
  429.                 WARN("Problem getting icon family: %d ", err);
  430. #endif
  431.             file->CloseResFile();
  432.         }
  433.     }
  434. }
  435.  
  436.